#! /bin/bash
# Generating
#  * channel-artifacts
#    * orderer.genesis.block
#    * channel.tx
#    * Org1MSPanchors.tx
#    * Org2MSPanchors.tx

if [ -f ./func.sh ]; then
  source ./func.sh
elif [ -f scripts/func.sh ]; then
  source scripts/func.sh
else
  echo "Cannot find the func.sh files, pls check"
  exit 1
fi

echo_b "Generating channel artifacts with ${GEN_IMG}"
[ ! -d ${CHANNEL_ARTIFACTS} ] && mkdir -p ${CHANNEL_ARTIFACTS}

echo_b "Make sure channel-artifacts dir exists already"
if [ -d ${CHANNEL_ARTIFACTS} -a ! -z "$(ls -A ${CHANNEL_ARTIFACTS})" ]; then
  echo_b "${CHANNEL_ARTIFACTS} exists, ignore."
  exit 0
fi

echo_g "Generating ${CHANNEL_ARTIFACTS}..."
docker run --rm -it \
  --name ${GEN_CONTAINER} \
  -e "FABRIC_LOGGING_SPEC=common.tools.configtxgen=DEBUG:INFO" \
  -v $PWD/${CRYPTO_CONFIG}:/tmp/${CRYPTO_CONFIG} \
  -v $PWD/configtx.yaml:/tmp/configtx.yaml \
  -v $PWD/${CHANNEL_ARTIFACTS}:/tmp/${CHANNEL_ARTIFACTS} \
  -v $PWD/scripts/variables.sh:/scripts/variables.sh \
  -v $PWD/scripts/gen_config_channel_docker.sh:/scripts/gen_config_channel_docker.sh \
  ${GEN_IMG} sh -c 'sleep 1; bash /scripts/gen_config_channel_docker.sh'
[ $? -ne 0 ] && exit 1

echo_g "Generate channel artifacts with $0 done"
